using System;
using EPiServer.Common;
using EPiServer.Common.Security;
using EPiServer.Community.DirectMessage;
using EPiServer.Community.DirectMessage.Queries;

namespace EPiAbstractions.Community.DirectMessage
{
    public class DirectMessageHandlerFacade : IDirectMessageHandlerFacade
    {
        private static DirectMessageHandlerFacade _instance;

        public DirectMessageHandlerFacade()
        {
            DirectMessageHandler.MessageSent += OnMessageSent;
            DirectMessageHandler.SystemMessageSent += OnSystemMessageSent;
        }

        public static DirectMessageHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new DirectMessageHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IDirectMessageHandlerFacade Members

        public event EPiServerCommonEventHandler MessageSent;
        public event EPiServerCommonEventHandler SystemMessageSent;

        public virtual MessageBase AddMessage(MessageBase message, FolderBase folder)
        {
            return DirectMessageHandler.AddMessage(message, folder);
        }

        public virtual MessageBase SendMessage(MessageBase message)
        {
            return DirectMessageHandler.SendMessage(message);
        }

        public virtual MessageBase SendMessage(MessageBase message, FolderBase copyFolder)
        {
            return DirectMessageHandler.SendMessage(message, copyFolder);
        }

        public virtual void UpdateMessage(MessageBase message)
        {
            DirectMessageHandler.UpdateMessage(message);
        }

        public virtual void RemoveMessage(MessageBase message)
        {
            DirectMessageHandler.RemoveMessage(message);
        }

        public virtual void RemoveMessage(MessageBase message, FolderBase folder)
        {
            DirectMessageHandler.RemoveMessage(message, folder);
        }

        public virtual MessageBase GetMessage(Int32 id)
        {
            return DirectMessageHandler.GetMessage(id);
        }

        public virtual MessageCollection GetMessages(FolderBase folder, Int32 page, Int32 pageSize, MessageSortOrder[] sortOrder)
        {
            return DirectMessageHandler.GetMessages(folder, page, pageSize, sortOrder);
        }

        public virtual MessageCollection GetMessages(FolderBase folder, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                     MessageSortOrder[] sortOrder)
        {
            return DirectMessageHandler.GetMessages(folder, page, pageSize, out totalItems, sortOrder);
        }

        public virtual void MoveMessage(MessageBase message, FolderBase fromFolder, FolderBase toFolder)
        {
            DirectMessageHandler.MoveMessage(message, fromFolder, toFolder);
        }

        public virtual FolderBase AddFolder(FolderBase folder)
        {
            return DirectMessageHandler.AddFolder(folder);
        }

        public virtual void UpdateFolder(FolderBase folder)
        {
            DirectMessageHandler.UpdateFolder(folder);
        }

        public virtual void RemoveFolder(FolderBase folder)
        {
            DirectMessageHandler.RemoveFolder(folder);
        }

        public virtual FolderBase GetFolder(Int32 id)
        {
            return DirectMessageHandler.GetFolder(id);
        }

        public virtual FolderCollection GetFolders(DirectMessageContainer dmc)
        {
            return DirectMessageHandler.GetFolders(dmc);
        }

        public virtual FolderCollection GetFolders(FolderBase parentFolder)
        {
            return DirectMessageHandler.GetFolders(parentFolder);
        }

        public virtual SystemFolder GetSystemFolder(DirectMessageContainer container, SystemFolderType type)
        {
            return DirectMessageHandler.GetSystemFolder(container, type);
        }

        public virtual void UpdateRecipient(MessageRecipient recipient)
        {
            DirectMessageHandler.UpdateRecipient(recipient);
        }

        public virtual void RemoveRecipient(MessageRecipient recipient)
        {
            DirectMessageHandler.RemoveRecipient(recipient);
        }

        public virtual MessageRecipient GetRecipient(MessageBase message, IUser user)
        {
            return DirectMessageHandler.GetRecipient(message, user);
        }

        public virtual MessageRecipientCollection GetRecipients(MessageBase message)
        {
            return DirectMessageHandler.GetRecipients(message);
        }

        public virtual DirectMessageContainer AddDirectMessageContainer(DirectMessageContainer dmc)
        {
            return DirectMessageHandler.AddDirectMessageContainer(dmc);
        }

        public virtual DirectMessageContainer GetDirectMessageContainer(Int32 id)
        {
            return DirectMessageHandler.GetDirectMessageContainer(id);
        }

        public virtual DirectMessageContainer GetDirectMessageContainer(IUser user, ISite site)
        {
            return DirectMessageHandler.GetDirectMessageContainer(user, site);
        }

        public virtual DirectMessageContainer GetDirectMessageContainer(IUser user, ISite site, Boolean createContainer)
        {
            return DirectMessageHandler.GetDirectMessageContainer(user, site, createContainer);
        }

        public virtual MessageCollection GetQueryResult(MessageBaseQuery query)
        {
            return DirectMessageHandler.GetQueryResult(query);
        }

        public virtual MessageCollection GetQueryResult(MessageBaseQuery query, Int32 page, Int32 pageSize)
        {
            return DirectMessageHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual MessageCollection GetQueryResult(MessageBaseQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return DirectMessageHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual FolderCollection GetQueryResult(FolderBaseQuery query)
        {
            return DirectMessageHandler.GetQueryResult(query);
        }

        public virtual FolderCollection GetQueryResult(FolderBaseQuery query, Int32 page, Int32 pageSize)
        {
            return DirectMessageHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual FolderCollection GetQueryResult(FolderBaseQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return DirectMessageHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnMessageSent(String sender, EPiServerCommonEventArgs e)
        {
            if (MessageSent != null)
                MessageSent(sender, e);
        }

        public virtual void OnSystemMessageSent(String sender, EPiServerCommonEventArgs e)
        {
            if (SystemMessageSent != null)
                SystemMessageSent(sender, e);
        }
    }
}